<template>
  <div>
    <a-layout id="components-layout-demo-fixed-sider" style="min-height: 100vh">
      <f-sider></f-sider>
      <a-layout>
        <f-header></f-header>
        <a-row type="flex" style="flex: auto">
          <a-row>
            <f-articlesider></f-articlesider>
          </a-row>
          <a-layout-content
            :style="{ margin: '24px 16px 0', overflow: 'initial' }"
          >
            <div :style="{ padding: '24px', background: '#fff' }">
              <a-row type="flex">
                <div class="title mr1r">文章管理</div>
              </a-row>
              <a-row class="mt1r" type="flex" justify="space-between">
                <a-col class="disflex">
                  <div>
                    <a-input-search
                      v-model="searchtitle"
                      placeholder="文章搜索"
                      style="width: 200px"
                      enterButton
                      @search="onSearch"
                    />
                  </div>
                  <!-- <a-button type="primary" @click="addcate()">创建新文章分类</a-button> -->
                </a-col>
                <a-col>
                  <a-button type="primary" @click="add()">创建新文章</a-button>
                </a-col>
              </a-row>
              <a-table
                class="mt1r"
                :columns="columns"
                :rowKey="(data) => data.aid"
                :dataSource="data"
                :pagination="pagination"
              >
                <template slot="articlecate" slot-scope="data">
                  <span>{{
                    data.article_cate_title
                      ? data.article_cate_title
                      : "顶级分类"
                  }}</span>
                </template>
                <template slot="articleimg" slot-scope="data">
                  <img :src="data.file_name" />
                </template>
                <template slot="operation" slot-scope="data">
                  <a class="operation" @click="edit(data.aid, data.title)"
                    >修改</a
                  >
                  <a class="operation" @click="del(data.aid)">删除</a>
                </template>
              </a-table>
            </div>
            <f-footer></f-footer>
          </a-layout-content>
        </a-row>
      </a-layout>
    </a-layout>
  </div>
</template>
<script>
import articlesider from "../../components/layout/ArticleSider.vue";
import sider from "../../components/layout/Sider.vue";
import header from "../../components/layout/Header.vue";
import footer from "../../components/layout/Footer.vue";
const columns = [
  {
    title: "文章ID",
    dataIndex: "aid",
  },
  {
    title: "所属分类",
    scopedSlots: { customRender: "articlecate" },
  },
  {
    title: "文章名称",
    dataIndex: "article_title",
  },
  {
    title: "缩略图",
    scopedSlots: { customRender: "articleimg" },
  },
  {
    title: "创建时间",
    dataIndex: "article_create_time",
  },
  {
    title: "操作",
    scopedSlots: { customRender: "operation" },
  },
];

const data = [];
export default {
  metaInfo: {
    title: "文章列表",
  },
  data() {
    return {
      data,
      columns,
      searchtitle: "",
      issearch: false,
      title: "",
      page: 1,
      pageSize: 8,
      pagination: {
        defaultPageSize: 8,
        total: 0,
        showTotal: (total) => `共 ${total} 条数据`,
        onChange: (page, pageSize) => this.changePage(page, pageSize), //点击页码事件
      },
    };
  },
  components: {
    "f-header": header,
    "f-sider": sider,
    "f-articlesider": articlesider,
    "f-footer": footer,
  },

  mounted() {
    this.$parent.tokenverify();
    this.getlst();
    this.setsiders();
  },
  methods: {
    addcate() {
      this.$router.push("/Article/articlecatemanage");
    },
    add() {
      this.$router.push("/Article/addarticle");
    },
    edit(aid) {
      this.$router.push({ path: "/Article/editarticle", query: { aid: aid } });
    },
    del(e) {
      var _this = this;
      this.$confirm({
        title: "该操作不可逆，确定删除该记录?",
        okText: "是的",
        okType: "danger",
        cancelText: "取消",
        onOk() {
          const url = "/Admin/Article/delarticle";
          const paramObj = {
            aid: e,
            token: localStorage.getItem("token"),
          };
          _this.$server.sl("GET", url, paramObj).then((res) => {
            if (res.code == 1) {
              _this.getlst();
              this.$message.success("删除成功");
            } else if (res.code == 4001) {
              _this.$router.push("/Auth/login");
              _this.$message.warning("凭证失效，请重新登陆");
            } else {
              _this.$message.warning(res.msg);
            }
          });
        },
        onCancel() {},
      });
    },

    //获取列表
    getlst() {
      const url = "/Admin/Article/articlemanage";
      const paramObj = {
        page: this.page,
        pageSize: this.pageSize,
        token: localStorage.getItem("token"),
      };
      this.$server.sl("GET", url, paramObj).then((res) => {
        if (res.code == 1) {
          this.data = res.data;
          this.pagination.total = res.count;
        } else if (res.code == 4001) {
          this.$router.push("/Auth/login");
          this.$message.warning("凭证失效，请重新登陆");
        } else {
          this.$message.warning("获取失败，请刷新重试");
        }
      });
    },
    //翻页
    changePage: function (page, pageSize) {
      this.page = page;
      this.pageSize = pageSize;
      if (this.issearch) {
        this.onSearch();
      } else {
        this.getlst();
      }
    },
    //搜索文章
    onSearch() {
      if (this.searchtitle) {
        this.issearch = true;
        const url = "/Admin/Article/Searcharticle";
        const paramObj = {
          title: this.searchtitle,
          page: this.page,
          pageSize: this.pageSize,
          token: localStorage.getItem("token"),
        };
        this.$server.sl("POST", url, paramObj).then((res) => {
          if (res.code == 1) {
            this.data = res.data;
            this.pagination.total = res.count;
          } else if (res.code == 4001) {
            this.$router.push("/Auth/login");
            this.$message.warning("凭证失效，请重新登陆");
          } else {
            this.$message.warning("获取失败，请刷新重试");
          }
        });
      } else {
        this.issearch = false;
      }
    },
    //菜单选中设置
    setsiders() {
      this.$store.commit("updateselectedKeys", "5");
      this.$store.commit("updateSubmenu", "5-1");
    },
  },
};
</script>
<style scoped>
#components-layout-demo-fixed-sider .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px;
}
.title {
  margin: 0 0 10px;
  font-size: 20px;
}
.picurl {
  height: 3rem;
}
.operation:first-child {
  margin-right: 1rem;
}
.disflex {
  display: flex;
}
img {
  height: 2rem;
  width: 2rem;
}
.mr1r {
  margin-right: 1rem;
}
.search {
  margin-right: 1rem;
  font-size: 16px;
}
</style>